#! /bin/sh
# PCP QA Test No. 212
# can I make pmdacisco dump core during exit()?
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

case $PCP_PLATFORM
in
    *)
	_notrun "This version of cisco PMDA does not use sprocs"
	# NOTREACHED
	;;
esac

status=0
_needclean=true
signal=$PCP_BINADM_DIR/pmsignal
trap "_cleanup" 0 1 2 3 15

_cleanup()
{
    if $_needclean
    then
	if [ "`echo $PMCD_LOG_DIR/core*`" != "$PMCD_LOG_DIR/core*" ]
	then
	    file $PMCD_LOG_DIR/core*
	fi
	rm -f $tmp.*
	$sudo ex - $PCP_PMCDCONF_PATH <<End-of-File
g/^#$seq#.*pmdacisco/s/^#$seq#//
w!
q
End-of-File
	_needclean=false
	_service pcp restart 2>&1 | _filter_pcp_start
	_wait_for_pmcd
	_wait_for_pmlogger
    fi
    exit $status
}

# real QA test starts here
status=1
$sudo rm -f $PMCD_LOG_DIR/core*

echo "SIGINT to pmdacisco parent ..."
for i in 1 2 3
do
    for j in 1 2 3 4 5
    do
	pid=`ps $PCP_PS_ALL_FLAGS | grep pmdacisco | grep -v grep \
	     | $PCP_AWK_PROG '
	    { pid[$2]++; ppid[$3]++; next }
    END	{ for (p in pid) {
		for (pp in ppid) {
		    if (p == pp) {
			print p
			exit
		    }
		}
	      }
	    }'`
	[ ! -z "$pid" ] && break
	sleep 1
    done

    if [ -z "$pid" ]
    then
	echo "Arrgh ... no pmdacisco parent process?"
	ps $PCP_PS_ALL_FLAGS | grep pmdacisco
	exit
    fi
    $sudo $signal -s TERM $pid
    sleep 5
    [ "`echo core*`" != "core*" ] && _cleanup
    echo "Restart with SIGHUP"
    $sudo $signal -a -s HUP pmcd
    sleep 5
done

echo "SIGINT to pmdacisco sproc ..."
for i in 1 2 3
do
    pid=`ps $PCP_PS_ALL_FLAGS | grep pmdacisco | grep -v grep \
	 | $PCP_AWK_PROG '
	{ pid[$2]++; ppid[$3]++; next }
END	{ for (p in pid) {
	    fail=0
	    for (pp in ppid) {
		if (p == pp) {
		    fail=1
		    break
		}
	    }
	    if (fail == 0) {
		print p
		exit
	    }
	  }
	}'`
    if [ -z "$pid" ]
    then
	echo "Arrgh ... no pmdacisco sproc process?"
	ps $PCP_PS_ALL_FLAGS | grep pmdacisco
	exit
    fi
    $sudo $signal -s TERM $pid
    sleep 5
    [ "`echo $PMCD_LOG_DIR/core*`" != "$PMCD_LOG_DIR/core*" ] && _cleanup
    echo "Restart with SIGHUP"
    $sudo $signal -a -s HUP pmcd
    sleep 5
done

echo
echo "start pmcd ..."
for i in 1 2 3
do
    rm -f $PMCD_LOG_DIR/core*
    if [ $1 -eq 1 ]
    then
	_service pcp start 2>&1 | _filter_pcp_start
    else
	_service pcp restart 2>&1 | _filter_pcp_start
    fi
    _wait_for_pmcd
    _wait_for_pmlogger
    [ "`echo $PMCD_LOG_DIR/core*`" != "$PMCD_LOG_DIR/core*" ] && _cleanup
done

echo
echo "stop pmcd ..."
for i in 1 2 3
do
    _service pcp start 2>&1 | _filter_pcp_start
    _wait_for_pmcd
    _wait_for_pmlogger
    rm -f $PMCD_LOG_DIR/core*
    _service pcp stop | _filter_pcp_stop
    [ "`echo $PMCD_LOG_DIR/core*`" != "$PMCD_LOG_DIR/core*" ] && _cleanup
done
_service pcp start 2>&1 | _filter_pcp_start
_wait_for_pmcd
_wait_for_pmlogger

echo
for i in 1 2 3
do
    echo "[pass $i] configure pmdacisco and notify pmcd ..."
    $sudo ex - $PCP_PMCDCONF_PATH <<End-of-File
g/^#$seq#.*pmdacisco/s/^#$seq#//
w!
q
End-of-File
    _service pcp restart 2>&1 | _filter_pcp_start
    _wait_for_pmcd
    _wait_for_pmlogger

    echo "[pass $i] unconfigure pmdacisco and notify pmcd ..."
    $sudo ex - $PCP_PMCDCONF_PATH <<End-of-File
g/^[^#].*pmdacisco/s/^/#$seq#/
w!
q
End-of-File
    rm -f $PMCD_LOG_DIR/core*
    $sudo $signal -a -s HUP pmcd
    [ "`echo $PMCD_LOG_DIR/core*`" != "$PMCD_LOG_DIR/core*" ] && _cleanup
done

echo
echo "all done ..."
status=0
exit
